package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockPurchase;
import com.zt.rongda.stock.service.StockPurchaseService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/stockPurchase")
public class StockPurchaseController {

    @Autowired
    private StockPurchaseService stockPurchaseService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockPurchaseService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseService.add(getStockPurchase(reqBean), reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseService.update(getStockPurchase(reqBean), reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseService.delete(reqBean.getInfo("listId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockPurchase getStockPurchase(ReqBean reqBean) {
        StockPurchase stockPurchase = new StockPurchase();
        stockPurchase.setListId(reqBean.getInfo("listId"));
        stockPurchase.setDeptId(reqBean.getInfo("deptId"));
        stockPurchase.setDeptName(reqBean.getInfo("deptName"));
        stockPurchase.setApplicant(reqBean.getInfo("applicant"));
        stockPurchase.setApprover(reqBean.getInfo("approver"));
        stockPurchase.setComment(reqBean.getInfo("comment"));
        stockPurchase.setOperator(reqBean.getUserId());
        stockPurchase.setRemark(reqBean.getInfo("remark"));
        return stockPurchase;
    }

}